package com.aso114.project.commonview;

import android.content.Context;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.aso114.project.R;
import com.aso114.project.base.BaseDialog;
import com.aso114.project.util.ScreenTools;

/**
 * Created by asus on 2018/8/9.
 */

public class NoticeDialog extends BaseDialog implements View.OnClickListener {

    private Context mContext;
    NoticeOnClick onClick;
    private String titles;
    private String text;
    private String btnText;

    public NoticeDialog(Context context, String title, String text, String btnText, NoticeOnClick onClick) {
        super(context);
        this.onClick = onClick;
        this.mContext = context;
        this.titles = title;
        this.text = text;
        this.btnText = btnText;
        init();
    }

    private void init() {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = this.getWindow();

            //设置修改状态栏
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);

            //设置状态栏的颜色，和你的app主题或者标题栏颜色设置一致就ok了
            window.setStatusBarColor(mContext.getResources().getColor(R.color.dialog));
        }
        View contentView = LayoutInflater.from(mContext).inflate(R.layout.dialog_notice, null);
        setContentView(contentView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        Window window = getWindow();
        // 设置显示动画
        WindowManager.LayoutParams wl = window.getAttributes();
        wl.x = 0;
        wl.y = ScreenTools.getScreenHeight(mContext);
        // 以下这两句是为了保证按钮可以水平满屏
        wl.width = ViewGroup.LayoutParams.MATCH_PARENT;
        wl.height = ViewGroup.LayoutParams.MATCH_PARENT;
        // 设置显示位置
        this.onWindowAttributesChanged(wl);
        this.setCancelable(false);
        this.setCanceledOnTouchOutside(false);
        contentView.findViewById(R.id.dialog_ok).setOnClickListener(this);
        contentView.findViewById(R.id.dialog_cancel).setOnClickListener(this);
        TextView title = (TextView) contentView.findViewById(R.id.dialog_title);
        TextView message = (TextView) contentView.findViewById(R.id.dialog_message);
        TextView btnOk = (TextView) contentView.findViewById(R.id.dialog_ok);
        title.setText(titles);
        message.setText(text);
        btnOk.setText(btnText);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.dialog_ok:
                if (onClick != null)
                    onClick.onclick();
                dismiss();
            case R.id.dialog_cancel:
                dismiss();
                break;
        }
    }

    public interface NoticeOnClick {
        void onclick();
    }
}
